package com.test.controller;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.test.R;
import com.test.adapter.NewsAdapter;
import com.test.adapter.NewsAdapterTwo;
import com.test.entity.News;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义列表控制
 *
 * @Author Musique
 * 2020/8/1
 */
public class ListViewActivity extends AppCompatActivity {

    private Context mContext;

    private List<News> listNew = null;

    private ListView listView;

    private NewsAdapter newsAdapter = null;

    private NewsAdapterTwo mNewsAdapterTwo = null;
    //第一种风格
    //    @Override
    //    public void onCreate(Bundle savedInstanceState) {
    //        super.onCreate(savedInstanceState);
    //        setContentView(R.layout.adapter_demo);
    //        mContext = this;
    //
    //        listView = findViewById(R.id.lv_data);
    //        listNew = new ArrayList<>();
    //        for (int i = 1; i < 25; i++) {
    //            News news = new News();
    //            news.setTitle("文章标题 -----" + i);
    //            news.setContent("文章内容 *******" + i);
    //            news.setAIcon(R.mipmap.pen);
    //            listNew.add(news);
    //            //  listNew.add(new News("123", "123", i));
    //        }
    //        newsAdapter = new NewsAdapter(listNew, mContext);
    //        listView.setAdapter(newsAdapter);
    //        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    //            @Override
    //            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    //                Toast.makeText(mContext, "点击文章第 " + i + " 次", Toast.LENGTH_LONG).show();
    //            }
    //        });
    //    }

    //第二种风格
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.adapter_demo);
        mContext = this;

        listView = findViewById(R.id.lv_data);
        listNew = new ArrayList<>();
        for (int i = 1; i < 25; i++) {
            News news = new News();
            news.setTitle("文章标题 ----- : " + i);
            news.setContent("文章内容 ******* : " + i);
            if (i % 2 == 0) {
                news.setAIcon(R.mipmap.pen1);
            } else {
                news.setAIcon(R.mipmap.iv_lol_icon1);
            }
            listNew.add(news);
            //  listNew.add(new News("123", "123", i));
        }
        mNewsAdapterTwo = new NewsAdapterTwo(listNew, mContext);
        listView.setAdapter(mNewsAdapterTwo);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(mContext, "点击文章第 " + i + " 次", Toast.LENGTH_LONG).show();
            }
        });
    }
}
